<template>
  <div>
    <LiangEditor :list="group" v-model:base-info="pageInConfig" @save="save" :is-show-lou="false"></LiangEditor>
  </div>
</template>

<script setup lang="ts">
import LiangEditor from "@/components/Editor/index.vue";
import { onBeforeMount, reactive, ref } from "vue";
import { useRoute } from "vue-router";
import { getGroups, getActive, createActive, editActive } from "@/api/modules/active";
const route = useRoute();

// 当前的文章配置
const pageInConfig = reactive({ title: "", cover: "", cont: "", author: "", group: undefined, hasVideo: false });

// 获取当前楼盘id以及文章id
const articleConfig = reactive({ id: 0, louPanId: 0 });
// 获取分类
interface Group {
  value: any;
  label: string;
}
const group = ref<Group[]>([]);

// 获取文章配置
const getArticleConfig = async () => {
  try {
    const { data } = await getGroups();
    const newData = data as any;
    group.value = newData.map((e: any) => {
      return { value: e.id, label: e.name };
    });
  } catch (error) {}
};

// 获取文章
const getArticleInfo = async () => {
  if (articleConfig.id === 0) return;
  try {
    const { data } = await getActive(articleConfig.id);
    const newData = data as any;
    pageInConfig.author = newData.author;
    pageInConfig.title = newData.title;
    pageInConfig.cont = newData.cont.content;
    pageInConfig.group = newData.group;
    pageInConfig.cover = newData.cover;
    pageInConfig.hasVideo = newData.hasVideo;
  } catch (error) {}
};
onBeforeMount(() => {
  if (route.query.id) {
    articleConfig.id = parseInt(route.query.id as string);
  }
  if (route.query.louPanId) {
    articleConfig.louPanId = parseInt(route.query.louPanId as string);
  }

  Promise.all([getArticleConfig(), getArticleInfo()]);
});

// 发布文章
const save = (val: any) => {
  const data: any = {
    title: val.title,
    author: val.author,
    cont: val.cont,
    group: val.group,
    cover: val.cover,
    sort: 1,
    louPanId: articleConfig.louPanId
  };
  if (articleConfig.id === 0) {
    createActive(data);
  } else {
    data.id = articleConfig.id;
    editActive(data);
  }
};
</script>

<style></style>
